//
//  CYPickerViewController.m
//  CaiYun
//
//  Created by 今日信息科技 on 2017/7/11.
//  Copyright © 2017年 今日信息科技. All rights reserved.
//

#import "CYPickerController.h"
#import "CYPickerDatasourceDelegate.h"
#import "CYTime.h"
@interface CYPickerController (){
    dateBlock _myblock;
}
@property (nonatomic, strong) CYLabel *titleLBL;
@property (nonatomic, strong) CYButton *cancelBtn;
@property (nonatomic, strong) CYButton *enterBtn;
@property (nonatomic, strong) CYView *toolView;
@property (nonatomic, strong) UIPickerView *picker;
@property (nonatomic, strong) CYPickerDatasourceDelegate *datasourceDelegate; //uipickerview的数据源代理
@property (nonatomic, copy) NSString *selectStr;//选中的日期


@end

@implementation CYPickerController
- (instancetype)initWithBlock:(dateBlock)block{
    self = [super init];
    if (self) {
        _myblock = block;
        self.backgroundColor = [UIColor whiteColor];
        self.bounds = CGRectMake(0.0, 0.0, SCREEN_WIDTH, AL(PT(460)));
        self.toolView.hidden = self.cancelBtn.hidden = self.enterBtn.hidden = self.titleLBL.hidden = self.picker.hidden = NO;
    }
    return self;
}

- (CYView *)toolView{
    if (!_toolView) {
        _toolView = [[CYView alloc]initWithColor:ColorHex(0xe0e0e0)];
        [self addSubview:_toolView];
        
        [_toolView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(0);
            make.centerX.equalTo(self);
            make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, AL(PT(90))));
        }];
    }
    return _toolView;
}

- (CYButton *)cancelBtn{
    if (!_cancelBtn) {
        _cancelBtn = [[CYButton alloc]initWithTitle:@"取消" titleColor:ColorHex(0x888888) backgroundColor:[UIColor clearColor] cornerRadius:0.0];
        _cancelBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(26))];
        [_cancelBtn addTarget:self action:@selector(actionForCancelBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_cancelBtn];
        
        [_cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(0);
            make.left.equalTo(self).with.offset(AL(PT(20)));
            make.size.mas_equalTo(CGSizeMake(AL(PT(90)), AL(PT(90))));
        }];
    }
    return _cancelBtn;
}

- (CYButton *)enterBtn{
    if (!_enterBtn) {
        _enterBtn = [[CYButton alloc]initWithTitle:@"确定" titleColor:ColorHex(0xff5b05) backgroundColor:[UIColor clearColor] cornerRadius:0.0];
        _enterBtn.titleLabel.font = [UIFont systemFontOfSize:AL(PT(26))];
        [_enterBtn addTarget:self action:@selector(actionForEnterBtn) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_enterBtn];
        
        [_enterBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self).with.offset(0);
            make.right.equalTo(self.mas_right).with.offset(-AL(PT(20)));
            make.size.mas_equalTo(CGSizeMake(AL(PT(90)), AL(PT(90))));
        }];
    }
    return _enterBtn;
}

- (CYLabel *)titleLBL{
    if (!_titleLBL) {
        _titleLBL = [[CYLabel alloc]initWithText:@"选择时间" size:AL(PT(30)) textcolor:ColorHex(0x333333)];
        [_toolView addSubview:_titleLBL];
        
        [_titleLBL mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo(_toolView);
            make.size.mas_equalTo(_titleLBL.bounds.size);
        }];
    }
    return _titleLBL;
}

- (UIPickerView *)picker{
    if (!_picker) {
        _picker = [[UIPickerView alloc]init];
        [self setupPicker];
        [self addSubview:_picker];
        
        [_picker mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_offset(UIEdgeInsetsMake(AL(PT(90)), 0, 0, 0));
        }];
        
    }
    return _picker;
}

- (void)setTypeStr:(NSString *)typeStr{
    if (_typeStr != typeStr)
        _typeStr = typeStr;
    _datasourceDelegate.type = ([typeStr isEqualToString:@"date"])?datePicker:areaPicker;
    _titleLBL.text = ([typeStr isEqualToString:@"date"])?@"选择时间":@"选择地区";
}

- (void)setSelectStr:(NSString *)selectStr{
    if (_selectStr != selectStr)
        _selectStr = selectStr;
    _myblock(selectStr, _datasourceDelegate.type,nil);

}
- (void)setupPicker{
    
    WS(self)
    _datasourceDelegate = [[CYPickerDatasourceDelegate alloc]initWithDidSelct:^(NSString *pickerStr) {
        if (pickerStr)//选中有值
           weakSelf.selectStr = pickerStr;
        else//未选中值，则设置默认选中的值
            [weakSelf setupPickerDefaultSelectRow];//设置picker默认显示
    }];
    _picker.dataSource = _datasourceDelegate.CYPickerDataSource;
    _picker.delegate = _datasourceDelegate.CYPickerDelegate;
    self.typeStr = @"date";//默认为时间选择器
}

- (void)setupPickerDefaultSelectRow{
    NSAssert(_typeStr.length > 0, @"必须设置pickerview的typestr,可以设置默认为date");
    [_picker reloadAllComponents];
    if (_datasourceDelegate.type == datePicker) {
        NSArray *currentDataArr = [CYTime timeArray];
        for (int i = 0 ; i < currentDataArr.count; i ++) {
            if (i < 2)
            [_picker.delegate pickerView:_picker didSelectRow:[currentDataArr[i] intValue] - 1 inComponent:i];
            else
                [_picker.delegate pickerView:_picker didSelectRow:[currentDataArr[i] intValue] inComponent:i];
        }
    }
    else if (_datasourceDelegate.type == areaPicker)
        for (int i = 0; i < 3; i ++) {
                        [_picker selectRow:0 inComponent:0 animated:YES];
            [_picker.delegate pickerView:_picker didSelectRow:0 inComponent:i];
        }

}

- (void)actionForEnterBtn{
    _myblock(_selectStr, _datasourceDelegate.type,@"enter");
    self.transform= CGAffineTransformIdentity;
}

- (void)actionForCancelBtn{
    _myblock(nil,_datasourceDelegate.type,@"cancel");
    self.transform= CGAffineTransformIdentity;
    
}


/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
